package com.hst.web.base.service;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import com.hst.core.dao.IORDao;
import com.hst.core.meta.MetaRepository;
import com.hst.web.base.entity.TReivewHistory;
import com.hst.web.base.ui.VehicleReview;

@Service
public class VehicleService {

	@Autowired
	IORDao dao;

	@Autowired
	MetaRepository mr;

	/**
	 * 导入车辆
	 * 
	 * @throws IOException
	 */
	public void importVehicle(MultipartFile file, Model m) throws IOException {
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		if (!".xls".equals(suffix) && !".xlsx".equals(suffix)) {
			throw new IOException("不支持该文件类型");
		}
		if (file.getSize() > 5 * 1024 * 1024) {
			throw new IOException("上传文件不得超过5MB");
		}
	}

	public void reviewApprove(List<VehicleReview> list, String remark) {
		for (VehicleReview e : list) {
			e.setReviewStatus("1");// 通过
			TReivewHistory h = new TReivewHistory();
			h.setVehicleCode(e.getVehicleCode());
			h.setReviewStatus(e.getReviewStatus());
			h.setReviewType("1");// 车辆复核
			h.setRemark(remark);
			dao.save(h);
			dao.update(mr.getMetaConvert("vehicle-review").toEntity(e));
		}
	}

	public void reviewReject(List<VehicleReview> list, String remark) {
		for (VehicleReview e : list) {
			e.setReviewStatus("2");// 不通过
			TReivewHistory h = new TReivewHistory();
			h.setVehicleCode(e.getVehicleCode());
			h.setReviewStatus(e.getReviewStatus());
			h.setReviewType("1");// 车辆复核
			h.setRemark(remark);
			dao.save(h);
			dao.update(mr.getMetaConvert("vehicle-review").toEntity(e));
		}
	}

}
